<script setup>
import {
  Search,
  Refresh,
  Plus,
  Delete,
  Download,
  Edit
} from '@element-plus/icons-vue'
import diaLog from '@/views/admin/components/plantingcostdialog.vue'
import { ref } from 'vue'
import { utils, writeFile } from 'xlsx'
import { plantingcostinfoGetService } from '@/api/admin/plantingcost'
const loading = ref(false)
const dialog = ref()
const plantingcostinfo = ref([])
const plantingcostinfoview = ref([])
//获取数据库数据
const getPlantingcostInfoList = async () => {
  loading.value = true
  const res = await plantingcostinfoGetService()
  plantingcostinfo.value = res.data.data
  plantingcostinfoview.value = res.data.data
  loading.value = false
}
getPlantingcostInfoList()
//重新渲染
const onSuccess = () => {
  getPlantingcostInfoList()
}
//添加操作
const addplantingoutinfo = () => {
  dialog.value.open({})
}
//编辑操作
const onEditUserinfo = (row) => {
  dialog.value.open(row)
}
import { plantingcostinfoDlService } from '@/api/admin/plantingcost'
//删除操作
const onDletplantingcostinfo = async (row) => {
  await ElMessageBox.confirm('是否删除该记录?', '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
    center: true
  })
  plantingcostinfoDlService(row.id)
    .then((response) => {
      if (response.data.status === 200) {
        ElMessage.success(response.data.message)
        getPlantingcostInfoList()
      } else {
        ElMessage.error(response.data.message)
      }
    })
    .catch((error) => {
      ElMessage.error(error)
    })
}
//搜索
const keyname = ref('')
const keycrops = ref('')
const keySearch = () => {
  plantingcostinfoview.value = []
  plantingcostinfoview.value = plantingcostinfo.value.filter((row) => {
    if (
      (keyname.value === '' || row.name.includes(keyname.value)) &&
      (keycrops.value === '' || row.crops.includes(keystatus.value))
    ) {
      return true // 符合搜索条件的保留
    } else {
      return false // 不符合搜索条件的过滤掉
    }
  })
}
//重置搜索条件
const resetFilter = () => {
  plantingcostinfoview.value = plantingcostinfo.value
  keyname.value = ''
  keycrops.value = ''
}
//导出数据
const table = ref(null)
const exportExcel = () => {
  if (table.value) {
    const sheet = utils.table_to_sheet(table.value.$el)
    const workbook = utils.book_new()
    utils.book_append_sheet(workbook, sheet, 'Sheet1')
    writeFile(workbook, 'plantingcostinfo_data.xlsx')
  }
}
</script>

<template>
  <page-container title="用户信息">
    <!-- 头部 -->
    <template #extra>
      <strong>菜地名称</strong>
      <el-input
        placeholder="输入关键字"
        style="width: 100px"
        v-model="keyname"
      /><strong>蔬菜品种</strong>
      <el-input
        placeholder="输入关键字"
        style="width: 100px"
        v-model="keycrops"
      />

      <el-button :icon="Search" @click="keySearch">搜索</el-button>
      <el-button :icon="Refresh" @click="resetFilter">重置</el-button>
      <el-button type="primary" plain :icon="Plus" @click="addplantingoutinfo"
        >新增</el-button
      >

      <el-button type="warning" plain :icon="Download" @click="exportExcel"
        >导出</el-button
      >
    </template>
    <el-table
      style="width: 100%"
      v-loading="loading"
      @selection-change="handleSelectionChange"
      ref="table"
      :data="plantingcostinfoview"
    >
      <el-table-column type="index" label="序号" width="100" />
      <el-table-column prop="name" label="菜地名称" width="150" />
      <el-table-column prop="crops" label="蔬菜品种" width="150" />
      <el-table-column prop="seedcosts" label="种子费用(元)" width="150" />
      <el-table-column
        prop="fertilizercosts"
        label="肥料费用(元)"
        width="150"
      />
      <el-table-column prop="pesticidecosts" label="农药费用(元)" width="150" />
      <el-table-column prop="laborcosts" label="工人费用(元)" width="150" />
      <el-table-column prop="othercosts" label="其他费用(元)" width="150" />
      <el-table-column prop="totalcosts" label="总费用(元)" width="150" />
      <el-table-column prop="remarks" label="备注" width="150" />
      <el-table-column fixed="right" label="操作" width="140">
        <template #default="{ row, $index }">
          <el-button
            type="primary"
            :icon="Edit"
            size="small"
            circle
            plain
            @click="onEditUserinfo(row, $index)"
          ></el-button>
          <el-button
            type="danger"
            size="small"
            :icon="Delete"
            circle
            plain
            @click="onDletplantingcostinfo(row, $index)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据"></el-empty>
      </template>
    </el-table>
    <diaLog ref="dialog" @success="onSuccess"></diaLog>
  </page-container>
</template>
<style>
.disabled-cell {
  color: #ccc;
}
</style>
